
export function debounce(func, wait, immediate = true) {
    let timeout;
    return function () {
        const context = this, args = arguments;
        const later = function () {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        const callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
}

export default class core {
    constructor() {
        this.initSafeDistance()
        this.registerEvent()

    }
    initSafeDistance() {
        window.safeTop = getComputedStyle(document.documentElement).getPropertyValue("--safeTop");
        window.safeTop = parseInt(window.safeTop.replace('px', ''))
        window.safeBottom = getComputedStyle(document.documentElement).getPropertyValue("--safeBottom")
        window.safeBottom = parseInt(window.safeBottom.replace('px', ''))

    }
    registerEvent() {
        const pageShowEvent = new CustomEvent("pageshow", { detail: 'APP_EVENT' });
        const pageBackEvent = new CustomEvent("pageback", { detail: 'APP_EVENT' });
        window.__pageCallback__ = (state, params = '{}') => {
            switch (state) {
                case 'BACK':
                    window.dispatchEvent(pageBackEvent)
                    break;
                case 'PAGESHOW':
                    window.dispatchEvent(pageShowEvent)
                    break;
                case 'SETLOCATION':
                    const pageGetLocationEvent = new CustomEvent("onlotation", { detail: params });
                    window.dispatchEvent(pageGetLocationEvent)
                    break;
                case 'ZOOMSTART':
                    const zoomstart = new CustomEvent("zoomstart", { detail: params });
                    window.dispatchEvent(zoomstart)
                    break;
                case 'ZOOMEND':
                    const zoomend = new CustomEvent("zoomend", { detail: params });
                    window.dispatchEvent(zoomend)
                    break;
                case 'MOVEEND':
                    const moveend = new CustomEvent("moveend", { detail: params });
                    window.dispatchEvent(moveend)
                    break;
                case 'MAP_CLICK':
                    const mapclick = new CustomEvent("mapclick", { detail: params });
                    window.dispatchEvent(mapclick)
                    break;
                case 'MAP_COMPLETE':
                    const mapcomplete = new CustomEvent("mapcomplete", { detail: params });
                    window.dispatchEvent(mapcomplete)
                    break;
                case 'MAP_LONG_PRESS':
                    const maplongtouch = new CustomEvent("maplongtouch", { detail: params });
                    window.dispatchEvent(maplongtouch)
                    break;
                case 'MAP_COMPLETE_DPPAGE':
                    const mapdpcomplete = new CustomEvent("mapdpcomplete", { detail: params });
                    window.dispatchEvent(mapdpcomplete)
                    break;
                case 'PAY_SUCCESS':
                    const paysuccess = new CustomEvent("paysuccess", { detail: 'success' });
                    window.dispatchEvent(paysuccess)
                    break;
                case 'PAY':
                    const pay = new CustomEvent("pay", { detail: params });
                    window.dispatchEvent(pay)
                    break;
                case 'TONGTONGPAY':
                    const tongtongpay = new CustomEvent("tongtongpay", { detail: params });
                    window.dispatchEvent(tongtongpay)
                    break;
                case 'MAP_CLICK_END':
                    const mapclickend = new CustomEvent("mapclickend", { detail: 'success' });
                    window.dispatchEvent(mapclickend)
                    break;
                case 'NET_STATE':
                    const netstate = new CustomEvent("netstate", { detail: params });
                    window.dispatchEvent(netstate)
                    break;
                case 'LOCATION_STATE':
                    const locationstate = new CustomEvent("locationstate", { detail: params });
                    window.dispatchEvent(locationstate)
                    break;
                case 'LOGIN_INFO':
                    if (typeof params == 'string') {
                        params = JSON.parse(params)
                    }
                    const logininfo = new CustomEvent("logininfo", { detail: params });
                    window.dispatchEvent(logininfo)
                    break;
                case 'CALL_EXPLORE':
                    const callexplore = new CustomEvent("callexplore", { detail: params });
                    window.dispatchEvent(callexplore)
                    break;
                case 'CALL_RED_PACKAGE':
                    const callredpackage = new CustomEvent("callredpackage", { detail: params });
                    window.dispatchEvent(callredpackage)
                    break;

            }
        }

    }
    defineFunc() {
        window.TongBridge.paths = {}
        window.TongBridge._goto = (path, params) => {
            TongBridge.newWebview(window.origin + path)
        }
        window.TongBridge._gotonft = (path) => {
            let origin = "https://nft-h5.xtt.xyz";
            if (process.env.NODE_ENV !== 'production') {
                origin = "https:/test-nft-h5.xtt.xyz";
            }
            TongBridge.newWebview(origin + path)
        }
        window.TongBridge.goto = debounce(window.TongBridge._goto, 300)
        window.TongBridge.gotonft = debounce(window.TongBridge._gotonft, 300)
        window.TongBridge._openModalNewWebview = (path, params) => {
            if (TongBridge.clientInfo.bridgeVersion) {
                if (TongBridge.clientInfo.bridgeVersion < 2) {
                    TongBridge.modalNewWebview(window.origin + path)
                } else {
                    TongBridge.modalNewWebview(JSON.stringify({
                        url: window.origin + path,
                        ...params
                    }))
                }
            } else {
                TongBridge.modalNewWebview(window.origin + path)
            }
        }
        window.TongBridge.openModalNewWebview = debounce(window.TongBridge._openModalNewWebview, 300)

        window.TongBridge._toShowDppage = (path, params) => {
            TongBridge.showDppage(JSON.stringify({
                url: window.origin + path,
                ...params
            }))
        }

        window.TongBridge.toShowDppage = debounce(window.TongBridge._toShowDppage, 300)
    }
}